package com.derbtec.refale.transaction;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.util.Log;

import com.derbtec.refale.R;
import com.derbtec.refale.activities.MyEventListViewActivity;
import com.derbtec.refale.util.Session;

public class GetMyEventParticipantsAsyncTask extends AsyncTask<String, Integer, Boolean> {
	
	private Activity activity;
	private ProgressDialog pdia;
	private int eventid, position;
	private int participantscount;
	private Boolean participantself = false;
	
	public GetMyEventParticipantsAsyncTask(Activity activity, int eventid, int position) {
		this.activity = activity;
		this.eventid = eventid;
		this.position = position;
	}
	
	protected void onPreExecute(){ 
		   super.onPreExecute();
		        pdia = new ProgressDialog(((MyEventListViewActivity) activity).getParent());
		        pdia.setMessage("Loading...");
		        pdia.setTitle(R.string.participation);
		        pdia.show();
		        //pdia.getWindow().setGravity(Gravity.CENTER);
		}
	
	protected Boolean doInBackground(String... params) {
		try {
			
			//Setup HttpConnectionsettings
			HttpParams httpparams = new BasicHttpParams();
			HttpProtocolParams.setVersion(httpparams, HttpVersion.HTTP_1_1);
			DefaultHttpClient client = new DefaultHttpClient(httpparams);
			HttpPost post = new HttpPost("http://derb.square7.ch/jsoneventparticipantsget.php");
			post.setHeader("Accept", "application/json");
			post.setHeader("Content-type", "application/json");
			
			//Writing Cookie in header for authentication
			post.setHeader("Cookie", Session.getCookie());
			
			//generate JSON-Event-Object
			JSONObject jsonobject = new JSONObject();
			jsonobject.put("eventid", eventid);
			
			//put object in array for syntax completion
			JSONArray jsonarray = new JSONArray();
			jsonarray.put(jsonobject);
			
			StringEntity entity = new StringEntity(jsonarray.toString());
			post.setEntity(entity);
			Log.d("AddEventJSONSENT", jsonarray.toString());
			//sending request and get response
			HttpResponse response = client.execute(post);
			
			Log.d("HTTPSTATUS", ""+response.getStatusLine().getStatusCode());
			
			if (response.getStatusLine().getStatusCode() == 200){

				InputStream is = response.getEntity().getContent();
				BufferedReader bfr = new BufferedReader(new InputStreamReader(is));
				String line = bfr.readLine();
				String newline;
				while ((newline = bfr.readLine()) != null) {
					line += (newline  + "\n");
				}
				Log.d("JSONresponse", line);
				
	            JSONArray jArray = new JSONArray(line);
	            participantscount = jArray.length();
	            for(int i=0;i<jArray.length();i++)
	            {	            	
	            	JSONObject json_data 	= jArray.getJSONObject(i);  
	                int participantid 		= Integer.parseInt(json_data.getString("Userid"));
	            	if(Session.getUserid() == participantid)
	            		participantself = true;
	            }
	            

              
	           
			}

		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return true;
		
	}
	
	protected void onProgressUpdate(Integer... progress) {	    
	}
	
	protected void onPostExecute(Boolean result) {
		pdia.dismiss();
		((MyEventListViewActivity)activity).startDetailViewActivity(position, participantscount, participantself);
		
		
	}
}

